<?php
/**
 * Nhận luồng ứng dụng từ URI
 */
class Router
{
    protected $_actionPrefix = 'action';

    /**
     * Chuỗi URI
     * @var string
     */
    protected $_route = '';

    protected $_folder = '';
    protected $_controller = '';
    protected $_action = 'Index';
    protected $_params = array();

    /**
     * Khởi tạo URI
     */
    private function __construct($route)
    {
        $this->_route = $route;
        $this->_parseRoute();
    }

    public static function create($route = '')
    {
        if ($route == '')
        {
            $route = Router::fetchRouteFromUrl();
        }

        return new self($route);
    }

    public function getRoute()
    {
        return $this->_route;
    }

    public function getFolder()
    {
        return $this->_folder;
    }

    public function setController($controller)
    {
        $this->_controller = $controller;
    }

    public function getController()
    {
        return $this->_controller;
    }

    public function setAction($action)
    {
        $this->_action = $action;
    }

    public function getAction()
    {
        return $this->_action;
    }

    public function addParams($param)
    {
        if (!is_array($param))
        {
            $param = array($param);
        }

        foreach ($param as $value)
        {
            $this->_params[] = $value;
        }
    }

    public function getParams()
    {
        return $this->_params;
    }

    public function getControllerPath()
    {
        $path = APPLICATION_PATH . 'controller/';

        if ($this->_folder)
        {
            $path .= $this->_folder . '/';
        }

        $path .= $this->_controller . '.php';

        return $path;
    }

    public static function fetchRouteFromUrl()
    {
        // Get route
        $route = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['PHP_SELF']);
        $route = trim($route, '/');

        return $route;
    }

    protected function _parseRoute()
    {
        $route = $this->_route;

        if ($route == '')
        {
            $route = Config::get('default_controller') . '/index';
        }

        $segments = explode('/', $route);

        if (is_array($segments))
        {
            if (is_dir(APPLICATION_PATH .'controller/' . $segments[0]))
            {
                $this->_folder = current($segments);
                each($segments);
            }

            if ($controller = current($segments))
            {
                $this->setController($this->_convertStringToName($controller));
                each($segments);
            }

            if ($action = current($segments))
            {
                $this->setAction($this->_convertStringToName($action));
                each($segments);
            }

            $dot = Config::get('param_separator');

            while ($param = each($segments))
            {
                $paramParts = explode($dot, $param[1]);

                if (isset($paramParts[1]))
                {
                    $this->addParams($paramParts[1]);
                }
                else
                {
                    $this->addParams($paramParts[0]);
                }
            }
        }
    }

    public function _convertStringToName($str)
    {
        return str_replace('-', '', $str);
    }
}